package com.employ.admin.service;

import com.employ.admin.common.Result;
import com.employ.admin.entity.*;
import com.employ.admin.mapper.MenuDoMapper;
import com.employ.admin.mapper.MenuMapper;
import com.employ.admin.mapper.UserMapper;
import com.employ.admin.utils.BuildMenu;
import com.employ.admin.utils.BuildTree;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class MenuService {
    @Resource
    MenuMapper menuMapper;
    public List<MenuTree<Menu>> listSysMenuTree() {
        String idParam = "0";		                           // 自定义顶级结点
        List<MenuTree<Menu>> trees = new ArrayList<MenuTree<Menu>>();  // 存放结果
        List<Menu> menus = menuMapper.findMenus();             // 取出数据

        List<MenuTree<Menu>> children = new ArrayList<MenuTree<Menu>>();  // 存放结果
        Menu menu = new Menu();
        // 1. 初步处理：让原始数据具备我们自定的的tree的特征
        for (Menu sysMenu : menus) {
            MenuTree<Menu> menuTree = new MenuTree<Menu>();
            menuTree.setId(sysMenu.getId().toString());
            menuTree.setParentId(sysMenu.getParentId().toString());
            menuTree.setName(sysMenu.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("path", sysMenu.getPath());
            attributes.put("icon", sysMenu.getIcon());

            List<Menu> navs = new ArrayList<Menu>();  // 存放结果
            List<Menu> authorities = new ArrayList<Menu>();  // 存放结果

            menu.setId(sysMenu.getId());
            menu.setName("用户列表");

            navs.add(menu);
            authorities.add(menu);

            menuTree.setChildren(children);


//            Map<String, Object> navMaps = new HashMap<>(16);
//            navMaps.put("nav", navs);

//            Map<String, Object> authoritiesMaps = new HashMap<>(16);
//            authoritiesMaps.put("authorities", authorities);


            menuTree.setAttributes(attributes);
            menuTree.setNav(navs);
            menuTree.setAuthorities(authorities);

            trees.add(menuTree);
        }

        // 2. 递归处理：根据数据库实际情况调整顶级结点的parentId值，默认为0
        List<MenuTree<Object>> list = BuildMenu.buildList(trees, idParam);

        return (List<MenuTree<Menu>>)trees;
    }
}
